package com.us.usws.site.ws.entity.shared;

import java.util.ArrayList;
import java.util.List;

import com.us.usws.biz.ResourceURIReplacer;
import com.us.usws.biz.WSResourceURIReplacer;
import com.us.usws.common.EpServerConstants;
import com.us.usws.model.domain.Meeting;
import com.us.usws.model.domain.MeetingAttendee;
import com.us.usws.model.domain.epserver.EpMscServerInfo;
import com.us.usws.service.ro.EpMscInfoROService;
import com.us.usws.service.ro.MeetingAttendeeROService;
import com.us.usws.site.ws.entity.shared.meeting.WSSharedMeetingDetail;
import com.us.usws.site.ws.entity.shared.meeting.WSSharedMeetingReplayURL;
import com.us.usws.site.ws.entity.shared.meeting.WSSharedMeetingServer;
import com.us.usws.site.ws.entity.shared.meeting.WSSharedMeetingUser;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.ServiceFactory;

public class PackEntityConverter {

	/**
	 * get channel meeting detail
	 * @param eachM
	 * @return
	 */
	public static WSSharedMeetingDetail genWSSharedMeeting(Meeting m) {
		EpMscInfoROService epMscS = ServiceFactory.getInstance().getService(EpMscInfoROService.class);
		MeetingAttendeeROService atS = ServiceFactory.getInstance().getService(MeetingAttendeeROService.class);
		WSSharedMeetingDetail ret = new WSSharedMeetingDetail(m);

		//snapshot and url
		ret.setSnapshot(ResourceURIReplacer.getMeetingSnapshotURL(m.getUid(), m.getPicture()));
		ret.setUrl(WSResourceURIReplacer.getMeetingInfoUrl(m.getUid()));

		//presenter
		String pUid = m.getPresenterUid();
		String pName = CacheManager.getUserCache().getUserName(pUid);
		WSSharedMeetingUser pre = new WSSharedMeetingUser(pUid, pName);
		ret.setPresenter(pre);

		//meeting servers
		List<EpMscServerInfo> info = epMscS.getMeetingServerInfos();
		List<WSSharedMeetingServer> servers = new ArrayList<WSSharedMeetingServer>(info.size());
		String videoReplayUrl = null, whiteBoardReplayUrl = null;

		for (EpMscServerInfo s : info) {
			WSSharedMeetingServer one = new WSSharedMeetingServer(s);
			servers.add(one);

			String url = EpServerConstants.MEETING_SERVER_HOST + "/" + m.getConfRoomId() + "/" + m.getConfRoomId();
			if (s.getRole() == EpServerConstants.EP_MSC_INFO_ROLE_MVIDEO || s.getRole() == EpServerConstants.EP_MSC_INFO_ROLE_SVIDEO) {
				videoReplayUrl = url + ".mp4";
			} else if (s.getRole() == EpServerConstants.EP_MSC_INFO_ROLE_MWB || s.getRole() == EpServerConstants.EP_MSC_INFO_ROLE_SWB) {
				whiteBoardReplayUrl = url + ".xml";
				//add one more server
				WSSharedMeetingServer two = new WSSharedMeetingServer(s);
				two.setType(EpServerConstants.EP_WS_MEETING_SERVER_TYPE_WHITE_BOARD_FILE);
				servers.add(two);
			}
		}
		ret.setServers(servers);

		//replay url

		WSSharedMeetingReplayURL replay = new WSSharedMeetingReplayURL(videoReplayUrl, whiteBoardReplayUrl);
		ret.setReplayUrl(replay);

		//members
		List<MeetingAttendee> attendees = atS.getMeetingAttendeesByMeeting(m.getUid());
		ret.setAttendeesCount(attendees.size());
		List<WSSharedMeetingUser> att = new ArrayList<WSSharedMeetingUser>();
		for (MeetingAttendee ea : attendees) {
			String name = CacheManager.getUserCache().getUserName(ea.getAttendeeUid());
			att.add(new WSSharedMeetingUser(ea.getAttendeeUid(), name));
		}
		ret.setAttendees(att);

		return ret;
	}
}
